<template>
  <div>
    <h1>物品列表</h1>
    <el-table :data="item">
      <el-table-column prop="_id" label="ID" width="230"> </el-table-column>
      <el-table-column prop="icon" label="图标" width="230">
        <!-- 添加一个可以自由展示内容的标签 -->
        <template slot-scope="scope">
          <!-- 这里是你想自定义展示的内容 -->
          <!-- 一个图标：src是当前所在列的icon值，所以用[：]，为一个http接口 -->
          <!-- rem:字高度=>3rem就是3个字的高度的意思 -->
          <img :src="scope.row.icon" style="height:3rem">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="物品名称"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/items/edit/${scope.row._id}`)"
            size="small">编辑</el-button>
          <el-button @click="remove(scope.row)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "ItemsList",

  data() {
    return {
      item: [],
    };
  },
  methods: {
    async fetch() {
      const res = await this.$http1.get("rest/items");
      this.item = res.data;
    },
    async remove(row) {
      this.$confirm(`此操作将永久删除 ${row.name}, 是否继续?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http1.delete(`rest/items/${row._id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.fetch();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.fetch();
  },
};
</script>

<style>
</style>